package xyz.zhzby.cyzx.judge.util;

import lombok.NoArgsConstructor;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @author zby
 * @date 2022/02/04
 * @Description 生成随机字符串类.
 */
@NoArgsConstructor
public class DigestUtils {
    /**
     * 字符串生成模式枚举类.
     */
    public static enum Mode {
        ALPHA,
        ALPHANUMERIC,
        NUMERIC
    }

    /**
     * @Param [length, mode] [字符串长度, 字符串生成模式]
     * @Return java.lang.String [随机字符串]
     * @Author zby
     * @Date 2022/02/04
     * @Description 生成随机字符串.
     */
    public static String getRandomString(int length, Mode mode) {
        StringBuilder buffer = new StringBuilder();
        String characters = "";

        switch (mode) {
            case ALPHA:
                characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                break;
            case ALPHANUMERIC:
                characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                break;
            case NUMERIC:
                characters = "1234567890";
                break;
        }

        int charactersLength = characters.length();
        for (int i = 0; i < length; ++i) {
            double index = Math.random() * charactersLength;
            buffer.append(characters.charAt((int) index));
        }
        return buffer.toString();
    }

}
